<template>
  <basic-dialog
    :width="'40%'"
    :visible.sync="auditVisible"
    :title="'审核'"
    :handle-close="handleCloseDialog"
    :showFooter="false"
  >
    <el-form
      ref="formData"
      :model="formData"
    >
      <el-form-item label="审核意见:" prop="auditOpinion">
        <el-input
          type="textarea"
          v-model.trim="formData.auditOpinion"
          placeholder="请输入审核意见"
          show-word-limit
          maxlength="200"
          rows="4"
        />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button :loading="auditLoading" type="primary" @click="handleAudit(1)">同意</el-button>
      <el-button :loading="auditLoading" @click="handleAudit(0)">驳回</el-button>
    </el-row>
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import { auditAbnormalPurchaseService } from '@api/purchase/abnormalPurchase';

export default {
  components: {
    BasicDialog
  },
  props: {
    auditVisible: {
      type: Boolean,
      default: false
    },
    selectIds: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      auditLoading: false,
      formData: {
        auditOpinion: '',
      },
    };
  },
  methods: {
    // 关闭弹框
    handleCloseDialog () {
      this.$refs.formData.resetFields();
      this.$emit('update:auditVisible', false);
    },
    handleAudit (auditType) {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          const param = {
            auditType: auditType,
            idList: this.selectIds,
            ...this.formData
          };
          this.audit(param)
        }
      });
    },
    async audit (param) {
      const res = await auditAbnormalPurchaseService(
        param,
        state => {
          this.auditLoading = state
        }
      );
      if (res) {
        this.$message.success('操作成功')
        this.$emit('getTableData');
        this.handleCloseDialog();
      }
    },
  }
};
</script>
